package org.jmeter.tcp.helmet.client;

import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.jmeter.tcp.helmet.protobuf.ServerHelmet;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/11/23 16:08
 * @since
 */
public class MessageContext {

    public static Map<NioSocketChannel, CompletableFuture> map = new ConcurrentHashMap<>();

    public static CompletableFuture put(NioSocketChannel channel, CompletableFuture future){
        CompletableFuture oldFuture = map.putIfAbsent(channel, future);
        return oldFuture != null ? oldFuture : future;
    }

    public static void setResult(Channel channel, Object msg) {
        CompletableFuture future = map.remove(channel);
        if(future != null){
            future.complete(msg);
        }
    }

}
